<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>表格管理</title>
    <link rel="stylesheet" href="/static/css/layui.css">
</head>

<body>

    <!--&lt;!&ndash; 主体部分 &ndash;&gt;-->
    <!--<div class="layui-card">-->
    <!--    <div class="layui-card-body">-->

    <!--        <div class="layui-form">-->
    <!--            <div class="layui-form-item">-->
    <!--                <div class="layui-inline">-->
    <!--                    <div class="layui-input-inline">-->
    <!--                        <input type="text" id="class_id2" name="class_id" placeholder="班级" autocomplete="off"-->
    <!--                               class="layui-input">-->
    <!--                    </div>-->
    <!--                </div>-->
    <!--                <div class="layui-inline">-->
    <!--                    <button id="btn_search" class="layui-btn layui-icon  layui-icon-search" data-type="search">查询-->
    <!--                    </button>-->
    <!--                </div>-->
    <!--                <div class="layui-inline">-->
    <!--                    <button id="btn_refresh" class="layui-btn layui-icon  layui-icon-refresh" data-type="search">重置-->
    <!--                    </button>-->
    <!--                </div>-->
    <!--            </div>-->

    <!--        </div>-->

    <!--        <table id="manage_table" lay-filter="manage_table"></table>-->
    <!--    </div>-->
    <!--</div>-->

    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">班级：</label>
                        <div class="layui-input-inline">
                            <select id="class_id2" lay-verify="required" lay-search lay-filter="classFilter">
                                <option value="">请选择班级</option>
                                <option value="1">1班</option>
                                <option value="3">3班</option>
                            </select>
                        </div>
                    </div>


                    <div class="layui-inline">
                        <button id="btn_search" class="layui-btn layui-icon  layui-icon-search" data-type="search">
                            查询
                        </button>
                    </div>
                    <div class="layui-inline">
                        <button id="btn_refresh" class="layui-btn layui-icon  layui-icon-refresh" data-type="search">
                            重置
                        </button>
                    </div>
                </div>
            </div>

            <table id="manage_table" lay-filter="manage_table"></table>
        </div>
    </div>


    <script type="text/html" id="tableToolBar">
    <div class="layui-btn-container">
        <div class="layui-input-inline">
            <button class="layui-btn layui-icon layui-icon-add-1" lay-event="add">手动新增</button>
        </div>
        <div class="layui-input-inline">
            <form enctype='multipart/form-data' method='' class="layui-icon" id="students_list">
                <input type="file" name="file" id="FileUpload" class="layui-input-inline">
                {#                <input type="submit" value="upload" class="layui-btn layui-icon layui-input-inline">#}
                <button class="layui-btn layui-input-inline" lay-submit lay-filter="post_form2">提交名单</button>
            </form>
        </div>
    </div>
</script>


    <!-- 表格基本操作按钮 -->
    <script type="text/html" id="table_operation">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
    <!--其他操作-->
    <script type="text/html" id="other_operation">

    </script>

    <!-- 添加-编辑 弹出框模板 -->
    <!-- 新增,编辑表单 -->
    <script type="text/html" id="add_edit_layer">

    <form class="layui-form" lay-filter="layer_form" id="layer_form" action="" style="width: 400px; margin: 10px auto">
        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color: red;">*</span>学号:</label>
            <div class="layui-input-block">
                <input type="text" name="ID" lay-verify="number" lay-verType="tips" style="width: 250px"
                       placeholder="请输入学号" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color: red;">*</span>姓名:</label>
            <div class="layui-input-block">
                <input type="text" name="name" lay-verify="required" lay-verType="tips" style="width: 250px"
                       placeholder="请输入姓名" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color: red;">*</span>性别:</label>
            <div class="layui-input-block">
                <input type="text" name="sex" lay-verify="required" lay-verType="tips" style="width: 250px"
                       placeholder="请输入性别" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color: red;">*</span>行政班号:</label>
            <div class="layui-input-block">
                <input type="text" name="class_id1" lay-verify="required" lay-verType="tips" style="width: 250px"
                       placeholder="请输入行政班号" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color: red;">*</span>教学班号:</label>
            <div class="layui-input-block">
                <input type="text" name="class_id2" lay-verify="number" lay-verType="tips" style="width: 250px"
                       placeholder="请输入教学班号" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color: red;">*</span>邮箱:</label>
            <div class="layui-input-block">
                <input type="email" name="email" lay-verify="required" lay-verType="tips" style="width: 250px"
                       placeholder="请输入邮箱" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">其他信息:</label>
            <div class="layui-input-block">
                <input type="text" name="information" style="width: 250px"
                       placeholder="请输入其他信息（地址，爱好等）" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">密码:</label>
            <div class="layui-input-block">
                <input type="text" name="password" style="width: 250px"
                       placeholder="初始密码为123456" autocomplete="off" class="layui-input" disabled>
            </div>
        </div>

        <div class="layui-form-item" id="form_btn">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="post_form">保存</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</script>

    <!-- javascript 脚本 -->
    <script src="/static/layui.js"></script>
    <script>
        layui.use(['layer', 'form', 'table'], function () {
            var table = layui.table;

            var
                $ = layui.jquery,
                table = layui.table,
                form = layui.form,
                layer = layui.layer;

            var currentRow = {},
                currentOpt = "",
                saveUrl = "/student/save",
                updateUrl = "/student/update",
                deleteUrl = "/student/delete",
                listUrl = "/student/list1",
                addUrl = "/student/add_students_by_xsl"
            deleteId = "ID",
                layerIndex = 0;

            $(function () {
                // 查询按钮
                $("#btn_search").on("click", function () {
                    table.reload('manage_table', {
                        where: {
                            class_id2: $("#class_id2").val()
                        }
                        , page: {
                            curr: 1 //重新从第 1 页开始
                        }
                    });
                });
                $("#btn_refresh").on("click", function () {
                    initTable();
                });
                initTable();
            });

            function initTable() {
                // 初始化列表(表格)
                table.render({
                    elem: '#manage_table'
                    , url: listUrl //数据接口
                    , method: "GET"
                    , request: {
                        pageName: 'pageIndex',  // 页码的参数名称，默认：page
                        limitName: 'pageSize'   // 每页数据量的参数名，默认：limit
                    }
                    , page: {
                        layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'],     // 自定义分页布局
                        curr: 1,      // 设置默认起始页1
                        groups: 10,   //只显示10个连续页码,就是说显示10个可见页其他的省略
                        first: false, // 不显示首页
                        last: false   // 不显示尾页
                    }
                    , limit: 10
                    , limits: [10, 20, 30, 40, 50]
                    , toolbar: '#tableToolBar'
                    , defaultToolbar: []
                    , cellMinWidth: 100
                    , cols: [
                        [ //表头
                            { field: 'ID', title: '学号', align: 'center', width: 200 }
                            , { field: 'name', title: '姓名', align: 'center', width: 150 }
                            , { field: 'sex', title: '性别', align: 'center', width: 80 }
                            , { field: 'email', title: '邮箱', align: 'center', width: 200 }
                            , { field: 'class_id1', title: '行政班号', align: 'center', width: 120 }
                            , { field: 'class_id2', title: '教学班号', align: 'center', width: 120 }
                            , { field: 'information', title: '信息', align: 'center' }
                            , {
                                width: 160,
                                align: 'center',
                                title: '操作',
                                toolbar: '#table_operation',
                                fixed: 'right'
                            }
                        ]
                    ],
                    parseData: function (res) { //res 即为原始返回的数据
                        console.log(res)
                        return {
                            "code": res.code == 200 ? 0 : res.code, //解析接口状态
                            "msg": res.code == 200 ? "" : res.msg, //解析提示文本
                            "count": res.count, //解析数据长度
                            "data": res.data //解析数据列表
                        };
                    }
                });

                // 绑定表格bar 按钮事件
                table.on('tool(manage_table)', function (obj) {

                    if (obj.event === 'edit') {
                        console.log(obj)
                        currentRow = obj.data;
                        currentOpt = updateUrl;
                        layerIndex = layer.open({
                            title: "修改",
                            type: 1,
                            shadeClose: true,
                            content: document.querySelector("#add_edit_layer").innerHTML
                        });
                        form.val('layer_form', currentRow);
                        form.render();

                    } else if (obj.event === 'del') {
                        layer.confirm('确认删除该行吗？', function (index) {
                            $.ajax({
                                type: 'GET', //请求方式
                                url: deleteUrl + "/" + obj.data[deleteId], //请求接口地址
                                contentType: 'application/json;charset=utf-8',
                                success: function (result) { //针对请求结果进行处理
                                    console.log(result)
                                    console.log(result.code)
                                    if (result.code === 0) {
                                        alert("删除成功");
                                        layer.msg("删除成功！", { time: 500 });
                                        table.reload('manage_table');
                                    } else {
                                        layer.msg("删除失败！");
                                    }
                                }
                            });
                            layer.close(index)
                        })
                    }
                });

                //监听新增事件
                table.on('toolbar(manage_table)', function (obj) {
                    if (obj.event == 'add') {
                        currentRow = {};
                        currentOpt = saveUrl;
                        layerIndex = layer.open({
                            title: '新增',
                            type: 1,
                            shadeClose: true,
                            content: document.querySelector("#add_edit_layer").innerHTML
                        });
                        document.getElementById("layer_form").reset();

                        form.render();
                    }

                });

            }

            form.on('submit(post_form2)', function (data) {
                var fileObj = document.getElementById("FileUpload").files[0]; // js 获取文件对象
                var formFile = new FormData();
                if (fileObj) {
                    formFile.append("file", fileObj)
                }
                var data = formFile;
                console.log(data.get("file"))
                console.log("准备")
                $.ajax({
                    type: 'POST', //请求方式
                    url: addUrl, //请求接口地址
                    processData: false,//用于对data参数进行序列化处理 这里必须false
                    contentType: false, //必须
                    dataType: 'json',
                    data: data, //传送数据
                    success: function (result) { //针对请求结果进行处理
                        if (result.code === 0) {
                            alert("提交成功");
                            layer.msg("提交成功！", { time: 500 });
                            table.reload('manage_table');
                        } else {
                            layer.msg("提交失败！");
                        }
                    }
                });
                return false;
            });


            // 保存按钮事件
            form.on('submit(post_form)', function (data) {
                var postData = Object.assign(currentRow, data.field);
                for (var i in postData) {    // 清除值为null的属性
                    if (postData[i] === null) {
                        delete postData[i];
                    }
                }
                console.log(postData)
                $.ajax({
                    type: 'POST', //请求方式
                    url: currentOpt, //请求接口地址
                    contentType: 'application/json;charset=utf-8',
                    dataType: 'json',
                    data: JSON.stringify(postData), //传送数据
                    success: function (result) { //针对请求结果进行处理
                        if (result.code === 0) {
                            alert("添加成功");
                            layer.msg("添加成功！", { time: 500 });
                            table.reload('manage_table');
                            document.getElementById("layer_form").style.visibility = 'hidden'
                        } else {
                            layer.msg("添加失败！");
                        }
                    }
                });
                return false;
            });
        });
    </script>

</body>

</html>